<?php
    include "include/function.php";
    valid_user();
    include "include/header.php";
?>
<title>Contest List</title>
    <div id="headline">
        <div class="wrapper">
            <h2>Contest List</h2>
        </div>
    </div>
<div id="pagebody">
	<div class="wrapper">

<table class="center" id="contest_list">
<thead>
    <tr>
        <th width="100">ID</th>
	    <th width="450">Title</th>
	    <th width="200">Start time</th>
        <th width="100">Status</th>
    </tr>
</thead>
<tbody>
<?php
    $database = connect_database();
    $query = "select contest_id, title, start_time, end_time from `contests` order by contest_id desc";
    $res = mysql_query($query, $database) or die("query error");
    $now = time();
    
    while ($row = mysql_fetch_row($res)) {
        $start = strtotime($row[2]);
        $end = strtotime($row[3]);

        echo "<tr>";
        echo '<td>' . $row[0] . '</td>';
        echo '<td><a href=contest?contest_id=' . $row[0] . '>' . $row[1] . '</a></td>';
        echo '<td>' . $row[2] . '</td>';
        if ($now < $start)
            echo "<td>Not start yet</td>";
        else if (($now >= $start) && ($now < $end))
            echo "<td>Running</td>";
        else
            echo "<td>Finished</td>";
        echo "</tr>\n";
    }
?>
  
</tbody></table>

    </div>
</div>

<br/>

<?php
	include "include/footer.php";
?>

